$(document).ready(function() {
	
	$("#selectall").click(function() {
        if ($("#selectall").prop("checked") == true) {
            $(".chek_all").prop("checked", true);
        } else {
            $(".chek_all").prop("checked", false);
        }
    });
	
	$(".form_datetime").datepicker({
		format: 'yyyy-mm-dd'
	});
	
	$("#deal_stage").select2({
       placeholder: "Select Deal Stage",
	   width: '100%'
    });
	
	$("#deal_type").select2({
       placeholder: "Select Deal Type",
	   width: '100%'
    });
	
	$("#deal_clnt_id").select2({
       placeholder: "Select Client",
	   width: '100%'
    });
	
	
	// Client Popup
	$("#add_client").click(function() {
        var chkArray = [];
        $(".chek_all:checked").each(function() {
            chkArray.push($(this).val());
        });
        var selected;
        selected = chkArray.join(',');
        //$("#ac_property_id").val(selected);
        $("#myModal_client").modal('show');

    });
	
	$("#add_btn_client").click(function() {
		
        var clnt_first_name = $("#clnt_first_name").val();
		var clnt_middle_name = $("#clnt_middle_name").val();
		var clnt_last_name = $("#clnt_last_name").val();
		var clnt_email = $("#clnt_email").val();
		var clnt_phone = $("#clnt_phone").val();
		
        $("#error_add_client").hide();
		
		if (clnt_first_name == '') {
            $("#error_add_client").show();
            $("#error_add_client").html('Please enter first name');
            $("#clnt_first_name").css('border-color', 'red');
            return false;
        } else if (clnt_last_name == '') {
            $("#error_add_client").show();
            $("#error_add_client").html('Please enter last name');
            $("#clnt_last_name").css('border-color', 'red');
            return false;
        } else if (clnt_email == '') {
            $("#error_add_client").show();
            $("#error_add_client").html('Please enter email');
            $("#comp_industry").css('border-color', 'red');
            return false;
        } else if (clnt_phone == '') {
            $("#error_add_client").show();
            $("#error_add_client").html('Please enter phone no.');
            $("#clnt_phone").css('border-color', 'red');
            return false;
        } else {
            var postdata = {};
            var obj = $(this);
            postdata['clnt_first_name'] = clnt_first_name;
			postdata['clnt_middle_name'] = clnt_middle_name;
			postdata['clnt_last_name'] = clnt_last_name;
			postdata['clnt_email'] = clnt_email;
			postdata['clnt_phone'] = clnt_phone;
			
            jQuery.post(ADMIN_URL + 'deals/addclient', postdata, function(clnt_id) {
               
                    $("#success_add_client").show();
                    window.setTimeout(function() {
                         $(".chek_all").prop("checked", false);
                         $("#success_add_client").hide();
                         $("#myModal_client").modal('hide');
                    }, 3000);	
					
					var postdata = {};
					postdata['client_last_id'] = clnt_id;
					jQuery.post(ADMIN_URL + 'deals/getclientlist', postdata, function(result) {
					   $('#deal_clnt_id').html(result);
					   $("#deal_clnt_id").select2("val", clnt_id);
					});
               
            });
        }
    });

});